/**
 * 
 */
package vehicleLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import vehicleEntity.SaleVehicleEntity;
import vehicleEntity.VehicleDisplayable;
import vehicleService.SaleService;

/**
 * @author Arimarajan
 *
 */
public class SaleLogic {

	/**
	 * This method fetches the list of sale vehicles
	 * @param none 
	 * @Pre  Data values are  got and set from the Entity class
	 * @Post Data Values are then passed to Service class
	 * @return none
	 * @throws Exception 
	 */
	public ObservableList<VehicleDisplayable> fetchSaleVehicle() throws Exception{

		SaleService ss = new SaleService();

		ObservableList<VehicleDisplayable> list=FXCollections.observableArrayList();
		try {
			ResultSet rs=ss.fetchSaleVehicle();
			while(rs.next())
			{
				VehicleDisplayable vcc = new VehicleDisplayable(rs.getInt("vehicleID"),rs.getString("RegNo"),rs.getString("VIN"),rs.getString("vehicleType"), rs.getString("vehicleClass"), rs.getString("vehicleName"), rs.getDate("movedOn").toString(), rs.getDouble("saleAmount"));

				list.add(vcc);

			}


		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}

		ss.closeConnection();
		return list;
	}

	/**
	 * This method fetches the details of the vehicle with given vehicle ID
	 * @param VehicleId
	 * @Pre Vehicle Id retrieved from sellVehicle method of SellVehicleController class
	 * @Post Retrieved Vehicle Id is then passed to Sale Logic
	 * @return none
	 * @throws SQLException 
	 */

	public SaleVehicleEntity fetchSaleVehicleDetail_for_edit(int VehicleId) throws SQLException {
		System.out.println("Scene3--> Sale Logic --> fetchVehicleDetail_for_edit :"+VehicleId);
		SaleService saleserviceobj = new SaleService();
		SaleVehicleEntity saleEntity = null;
		ResultSet resultSet = null;
		try {
			resultSet = saleserviceobj.fetchSaleVehicleDetail_for_edit(VehicleId);
			
			if (resultSet!=null && resultSet.next()){
				saleEntity = new SaleVehicleEntity();
				//System.out.println("got the value");
				//System.out.println(resultSet.getInt("vehicleId"));
				//System.out.println(resultSet.getString("vehicleType"));  
				//saleEntity.setVehicleId(resultSet.getInt("vehicleID"));
				saleEntity.setRegNo(resultSet.getString("RegNo")); 
				saleEntity.setVin(resultSet.getString("VIN"));
				saleEntity.setVehicleType(resultSet.getString("vehicleType"));
				saleEntity.setVehicleClass(resultSet.getString("vehicleClass"));
				saleEntity.setVehicleName(resultSet.getString("vehicleName"));
				saleEntity.setMovedOn(resultSet.getDate("movedOn"));
				saleEntity.setSaleAmount(resultSet.getDouble("saleAmount"));
				System.out.println("moved date from fetchSaleVehicleDetail_for_edit : "+saleEntity.getMovedOn());
				saleserviceobj.closeConnection();
			}
			else{
				System.out.println("no value returned");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} 
		return saleEntity;
	}

	/**
	 * This method loads the editable form for sale list with values fetched from Service class
	 * @param VehicleId
	 * @Pre VehicleId is retrieved from the search field
	 * @Post VehicleId is passed to the SaleService class
	 * @return none
	 * @throws SQLException 
	 */

	//calling 2nd time for 2nd editable form
	public SaleVehicleEntity fetchSaleVehicleDetail_for_sale(int VehicleId) throws SQLException {
		SaleService saleserviceobj2 = new SaleService();
		ResultSet resultSet = null; 
		SaleVehicleEntity saleEntity = null;
		try{
			resultSet =saleserviceobj2.fetchSaleVehicleDetail_for_sale(VehicleId);
			if (resultSet!=null && resultSet.next()){
				saleEntity = new SaleVehicleEntity();
				saleEntity.setVehicleName(resultSet.getString("vehicleName"));
				saleEntity.setVehicleClass(resultSet.getString("vehicleClass"));
				saleEntity.setVehicleType(resultSet.getString("vehicleType"));
				saleEntity.setSaleAmount(resultSet.getDouble("saleAmount"));
				saleEntity.setSoldAmount(resultSet.getDouble("soldAmount"));
				saleEntity.setSoldTo(resultSet.getString("soldTo"));
				saleEntity.setSoldDate(resultSet.getDate("soldDate"));
				saleserviceobj2.closeConnection();

			}
			else{
				System.out.println("no value returned");
			}
		}

		catch(SQLException e){
			e.printStackTrace();
			throw e;
		}

		return saleEntity;
	}

	/**
	 * This method saves the updated value of the edited values in Sale vehicle and vehicle tables
	 * @param saleVehicle
	 * @Pre saleVehicle value should be retrieved from SellVehicleController class
	 * @Post Data Values will be sent to SellVehicleController class 
	 * @return null
	 * @throws SQLException 
	 */
	public String saveUpdated_for_edit(SaleVehicleEntity saleVehicle) throws SQLException{
		System.out.println("In sale logic  class --> saveUpdated_for_edit : "+saleVehicle.getVehicleClass());
		SaleService saleService = new SaleService();
		try {
			saleService.saveUpdated_for_query1(saleVehicle);
			saleService.saveUpdated_for_query2(saleVehicle);
			saleService.transactionCommit();
		} catch (SQLException e) {
			try {
				saleService.transactionRollBack();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw e1;
			}
			e.printStackTrace();
			throw e;
		}	
		
		return null;
	}

	/**
	 * This method updates the value in table when selling a vehicle
	 * @param saleVehicle
	 * @Pre saleVehicle value should be retrieved from SellVehicleController class
	 * @Post Data Values will be sent to SellVehicleController class 
	 * @return null
	 * @throws SQLException
	 */
	public String saveUpdated_for_sell(SaleVehicleEntity saleVehicle) throws SQLException {
		System.out.println("In sale logic  class --> saveUpdated_for_sale : "+saleVehicle.getVehicleClass());
		SaleService saleService = new SaleService();
		try {
			saleService.saveUpdated_for_sell(saleVehicle);
			saleService.transactionCommit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				saleService.transactionRollBack();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw e1;
			}
			throw e;
		}	


		return null;
	}


}
